package org.itboys.mobile.service.mongo.web;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.entity.mongo.SundayRecordPermission;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/10/26
 * 描述：雄安加盟网_记录权限_service
 */
@Service
public class SundayRecordPermissionService extends BaseMongoService<SundayRecordPermission>{
    @Resource(name="mobileDs")
    private MongoDataSource sundayDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }
    @Override
    protected Class<SundayRecordPermission> getEntityClass() {
        return SundayRecordPermission.class;
    }

    /**
     * 保存记录权限
     * @param memberRecordId
     * @param otherIds
     * @param otherId
     * @param type 1,商品，2分类，3其他
     * @param isOverwrite
     */
    public void saveRecordPermission(Long memberRecordId, List<Long> otherIds, Long otherId, Integer type, boolean isOverwrite){
        if(ListUtil.isNull(otherIds))otherIds=new ArrayList<Long>();
        if(LongUtil.isNotZreo(otherId))otherIds.add(otherId);
        if(isOverwrite){
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("isDeleted",0);
            param.put("type",type);
            param.put("memberRecordId",memberRecordId);
            super.delete(param);
        }
        if(ListUtil.isNotNull(otherIds)){
            List<SundayRecordPermission> permissions = new ArrayList<SundayRecordPermission>();
            for(Long o:otherIds){
                permissions.add(new SundayRecordPermission(memberRecordId,o,type));
            }

            if(ListUtil.isNotNull(permissions))super.batchSave(permissions);
        }
    }

    /**
     *获取权限
     * @param memberRecordId
     * @param type
     * @return
     */
    public List<Long> getRecordPermission(Long memberRecordId, Integer type){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted",0);
        param.put("type",type);
        param.put("memberRecordId",memberRecordId);
        List<Long> otherIds=new ArrayList<Long>();

        for(SundayRecordPermission permission:super.list(param)){
            otherIds.add(permission.getOtherId());
        }
        return otherIds;
    }
    /**
     * 通过产品id  获取持有者id
     * @param otherId
     * @return
     */
    public Long getMemberRecord(Long otherId){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted",0);
        param.put("otherId",otherId);
        List<SundayRecordPermission> permissions=super.list(param);
        if(ListUtil.isNotNull(permissions)){
            return permissions.get(0).getMemberRecordId();
        }else {
            return 0l;
        }
    }
}
